﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Runtime.Serialization.Json;
using System.Xml;
using System.IO;

namespace MvcApplicationYoung.Models
{
   public class ModelsCommon
    {
       public class ExcludeEntityKeyContractResolver : DefaultContractResolver
       {
           protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
           {
               IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
               return properties.Where(p => p.PropertyType != typeof(System.Data.EntityKey)).ToList();
           }
       }

       public static string XMLToJSON(object  obj)
        {

            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            settings.ContractResolver = new ExcludeEntityKeyContractResolver();
            string result = JsonConvert.SerializeObject(obj, settings);
           
            return result;
        }

    }
}
